﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace dotServer
{
	class Program
	{
		#region static fields

		static private Program prog = null;

		#endregion

		#region fields

		private ManualResetEvent eve;

		#endregion


		/// <summary>
		/// EntryPoint
		/// </summary>
		/// <param name="args"></param>
		static void Main ( string[] args )
		{
			try
			{
				prog = new Program();
			}catch( Exception exc )
			{
				Console.Error.Write( "exception caches rogram-root\r\n" );
				Console.Error.Write( exc.ToString() );
			}
		}


		/// <summary>
		/// Constractor
		/// </summary>
		public Program ()
		{
			eve = new ManualResetEvent( false );
			EchoCopyright();
		}


		/// <summary>
		/// Execute.
		/// </summary>
		private void Execute ()
		{
			try
			{
				Global.init();

				//	Ctrl＋Cの検出用
				Console.CancelKeyPress += new ConsoleCancelEventHandler( consoleHandle );

				//	設定を読み込んで、

				//	実行するのですよ

				//	実行が完了するまで待つ。
				eve.WaitOne();
				eve = null;
				Console.Out.WriteLine( "Finished." );

			} finally
			{
			}
			return;
		}


		/// <summary>
		/// handler for CTRL+C
		/// </summary>
		/// <param name="sender">送り主。使わない</param>
		/// <param name="e">イベントパラメタ</param>
		public void consoleHandle ( object sender, ConsoleCancelEventArgs e )
		{
			Console.Error.WriteLine( "shutdown...." );

			if ( eve!=null )
			{
				eve.Set();
			}
			//	強制終了はしない。正常終了させる。
			e.Cancel = true;
			return;
		}


		/// <summary>
		/// 著作権表示する
		/// </summary>
		private void EchoCopyright ()
		{
			System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
			System.Reflection.AssemblyName assName = assembly.GetName();

			//AssemblyCopyrightの取得
			System.Reflection.AssemblyCopyrightAttribute asmcpy =
				(System.Reflection.AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(
					System.Reflection.Assembly.GetExecutingAssembly(),
					typeof( System.Reflection.AssemblyCopyrightAttribute ) );

			//	著作権表示
			Console.Out.WriteLine( assName.Name + " Ver" + assName.Version + " " + asmcpy.Copyright );

			//	プラットフォーム表示してみようか
			Console.Out.WriteLine( System.Environment.OSVersion.Platform );
		}
	}
}
